import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/home/home_kingkong_model.dart';
import 'package:juming_test/module/home/kingkong_management/widgets/kingkong_management_item.dart';
import 'package:flutter_reorderable_grid_view/widgets/widgets.dart';

class KingkongManagementActiveSection extends StatelessWidget {
  const KingkongManagementActiveSection({
    super.key,
    required this.kingkongList,
    required this.isEditing,
    required this.onResetToDefault,
    required this.onEnterEdit,
    required this.onConfirmEdit,
    required this.onDragSort,
    required this.onRemove,
  });

  // 金刚位数据
  final List<HomeKingkongModel> kingkongList;
  // 编辑模式
  final bool isEditing;
  // 事件处理
  // 恢复默认
  final Function onResetToDefault;
  // 进入编辑
  final Function onEnterEdit;
  // 确认编辑
  final Function onConfirmEdit;
  // 拖拽排序
  final Function(List<HomeKingkongModel>) onDragSort;
  // 移除item
  final Function(HomeKingkongModel) onRemove;

  @override
  Widget build(BuildContext context) {
    return SliverPadding(
      padding: EdgeInsets.all(16),
      sliver: SliverToBoxAdapter(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text(
                  '首页功能',
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                if (isEditing)
                  Text(
                    '（长按可拖动排序）',
                    style: TextStyle(fontSize: 12.sp, color: Colors.grey),
                  ),
                Spacer(),
                TextButton(
                  onPressed: () => onResetToDefault(),
                  child: Text(
                    '恢复默认',
                    style: TextStyle(fontSize: 12.sp, color: Colors.black),
                  ),
                ),
                SizedBox(
                  width: 60.w,
                  height: 30.w,
                  child: ElevatedButton(
                    onPressed: () {
                      if (isEditing) {
                        onConfirmEdit();
                      } else {
                        onEnterEdit();
                      }
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Color(0xFF1563E7),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(15.w),
                      ),
                      padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                    ),
                    child: Text(
                      isEditing ? '保存' : '编辑',
                      maxLines: 1,
                      style: TextStyle(color: Colors.white, fontSize: 12.sp),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: 12),
            _buildActiveGrid(kingkongList, isEditing),
          ],
        ),
      ),
    );
  }

  // 已激活网格布局
  Widget _buildActiveGrid(List<HomeKingkongModel> items, bool isEditing) {
    final generatedChildren = List.generate(
      items.length,
      (index) => HomeKingkongManagementItem(
        key: Key("${items[index].id}"),
        isActive: true,
        kingkong: items[index],
        isEditing: isEditing,
        onRemove: (kingkong) => onRemove(kingkong),
      ),
    );
    return ReorderableBuilder(
      enableDraggable: isEditing,
      children: generatedChildren,
      onReorder: (ReorderedListFunction reorderedListFunction) {
        var list = reorderedListFunction(items);
        onDragSort(list.cast<HomeKingkongModel>());
      },
      builder: (List<Widget> children) {
        return GridView(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
            mainAxisSpacing: 8,
            crossAxisSpacing: 8,
            childAspectRatio: 1,
          ),
          children: children,
        );
      },
    );
  }
}
